<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>imehandler.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_imehandler.js.html">imehandler.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Input Method Editors (IMEs) are OS-level widgets that make
<a name="line17"></a> * it easier to type non-ascii characters on ascii keyboards (in particular,
<a name="line18"></a> * characters that require more than one keystroke).
<a name="line19"></a> *
<a name="line20"></a> * When the user wants to type such a character, a modal menu pops up and
<a name="line21"></a> * suggests possible &quot;next&quot; characters in the IME character sequence. After
<a name="line22"></a> * typing N characters, the user hits &quot;enter&quot; to commit the IME to the field.
<a name="line23"></a> * N differs from language to language.
<a name="line24"></a> *
<a name="line25"></a> * This class offers high-level events for how the user is interacting with the
<a name="line26"></a> * IME in editable regions.
<a name="line27"></a> *
<a name="line28"></a> * Known Issues:
<a name="line29"></a> *
<a name="line30"></a> * Firefox always fires an extra pair of compositionstart/compositionend events.
<a name="line31"></a> * We do not normalize for this.
<a name="line32"></a> *
<a name="line33"></a> * Opera does not fire any IME events.
<a name="line34"></a> *
<a name="line35"></a> * Spurious UPDATE events are common on all browsers.
<a name="line36"></a> *
<a name="line37"></a> * We currently do a bad job detecting when the IME closes on IE, and
<a name="line38"></a> * make a &quot;best effort&quot; guess on when we know it&#39;s closed.
<a name="line39"></a> *
<a name="line40"></a> */
<a name="line41"></a>
<a name="line42"></a>goog.provide(&#39;goog.events.ImeHandler&#39;);
<a name="line43"></a>goog.provide(&#39;goog.events.ImeHandler.Event&#39;);
<a name="line44"></a>goog.provide(&#39;goog.events.ImeHandler.EventType&#39;);
<a name="line45"></a>
<a name="line46"></a>goog.require(&#39;goog.events.Event&#39;);
<a name="line47"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line48"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line49"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line50"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line51"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line52"></a>goog.require(&#39;goog.userAgent.product&#39;);
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * Dispatches high-level events for IMEs.
<a name="line58"></a> * @param {Element} el The element to listen on.
<a name="line59"></a> * @extends {goog.events.EventTarget}
<a name="line60"></a> * @constructor
<a name="line61"></a> */
<a name="line62"></a>goog.events.ImeHandler = function(el) {
<a name="line63"></a>  goog.base(this);
<a name="line64"></a>
<a name="line65"></a>  /**
<a name="line66"></a>   * The element to listen on.
<a name="line67"></a>   * @type {Element}
<a name="line68"></a>   * @private
<a name="line69"></a>   */
<a name="line70"></a>  this.el_ = el;
<a name="line71"></a>
<a name="line72"></a>  /**
<a name="line73"></a>   * Tracks the keyup event only, because it has a different life-cycle from
<a name="line74"></a>   * other events.
<a name="line75"></a>   * @type {goog.events.EventHandler}
<a name="line76"></a>   * @private
<a name="line77"></a>   */
<a name="line78"></a>  this.keyUpHandler_ = new goog.events.EventHandler(this);
<a name="line79"></a>
<a name="line80"></a>  /**
<a name="line81"></a>   * Tracks all the browser events.
<a name="line82"></a>   * @type {goog.events.EventHandler}
<a name="line83"></a>   * @private
<a name="line84"></a>   */
<a name="line85"></a>  this.handler_ = new goog.events.EventHandler(this);
<a name="line86"></a>
<a name="line87"></a>  if (goog.events.ImeHandler.USES_COMPOSITION_EVENTS) {
<a name="line88"></a>    this.handler_.
<a name="line89"></a>        listen(el, &#39;compositionstart&#39;, this.handleCompositionStart_).
<a name="line90"></a>        listen(el, &#39;compositionend&#39;, this.handleCompositionEnd_).
<a name="line91"></a>        listen(el, &#39;compositionupdate&#39;, this.handleTextModifyingInput_);
<a name="line92"></a>  }
<a name="line93"></a>
<a name="line94"></a>  this.handler_.
<a name="line95"></a>      listen(el, &#39;textInput&#39;, this.handleTextInput_).
<a name="line96"></a>      listen(el, &#39;text&#39;, this.handleTextModifyingInput_).
<a name="line97"></a>      listen(el, goog.events.EventType.KEYDOWN, this.handleKeyDown_);
<a name="line98"></a>};
<a name="line99"></a>goog.inherits(goog.events.ImeHandler, goog.events.EventTarget);
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * Event types fired by ImeHandler. These events do not make any guarantees
<a name="line104"></a> * about whether they were fired before or after the event in question.
<a name="line105"></a> * @enum {string}
<a name="line106"></a> */
<a name="line107"></a>goog.events.ImeHandler.EventType = {
<a name="line108"></a>  // After the IME opens.
<a name="line109"></a>  START: &#39;startIme&#39;,
<a name="line110"></a>
<a name="line111"></a>  // An update to the state of the IME. An &#39;update&#39; does not necessarily mean
<a name="line112"></a>  // that the text contents of the field were modified in any way.
<a name="line113"></a>  UPDATE: &#39;updateIme&#39;,
<a name="line114"></a>
<a name="line115"></a>  // After the IME closes.
<a name="line116"></a>  END: &#39;endIme&#39;
<a name="line117"></a>};
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>
<a name="line121"></a>/**
<a name="line122"></a> * An event fired by ImeHandler.
<a name="line123"></a> * @param {goog.events.ImeHandler.EventType} type The type.
<a name="line124"></a> * @param {goog.events.BrowserEvent} reason The trigger for this event.
<a name="line125"></a> * @constructor
<a name="line126"></a> * @extends {goog.events.Event}
<a name="line127"></a> */
<a name="line128"></a>goog.events.ImeHandler.Event = function(type, reason) {
<a name="line129"></a>  goog.base(this, type);
<a name="line130"></a>
<a name="line131"></a>  /**
<a name="line132"></a>   * The event that triggered this.
<a name="line133"></a>   * @type {goog.events.BrowserEvent}
<a name="line134"></a>   */
<a name="line135"></a>  this.reason = reason;
<a name="line136"></a>};
<a name="line137"></a>goog.inherits(goog.events.ImeHandler.Event, goog.events.Event);
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * Whether to use the composition events.
<a name="line142"></a> * @type {boolean}
<a name="line143"></a> */
<a name="line144"></a>goog.events.ImeHandler.USES_COMPOSITION_EVENTS =
<a name="line145"></a>    goog.userAgent.GECKO ||
<a name="line146"></a>    (goog.userAgent.WEBKIT &amp;&amp; goog.userAgent.isVersion(532));
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Stores whether IME mode is active.
<a name="line151"></a> * @type {boolean}
<a name="line152"></a> * @private
<a name="line153"></a> */
<a name="line154"></a>goog.events.ImeHandler.prototype.imeMode_ = false;
<a name="line155"></a>
<a name="line156"></a>
<a name="line157"></a>/**
<a name="line158"></a> * The keyCode value of the last keyDown event. This value is used for
<a name="line159"></a> * identiying whether or not a textInput event is sent by an IME.
<a name="line160"></a> * @type {number}
<a name="line161"></a> * @private
<a name="line162"></a> */
<a name="line163"></a>goog.events.ImeHandler.prototype.lastKeyCode_ = 0;
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * @return {boolean} Whether an IME is active.
<a name="line168"></a> */
<a name="line169"></a>goog.events.ImeHandler.prototype.isImeMode = function() {
<a name="line170"></a>  return this.imeMode_;
<a name="line171"></a>};
<a name="line172"></a>
<a name="line173"></a>
<a name="line174"></a>/**
<a name="line175"></a> * Handles the compositionstart event.
<a name="line176"></a> * @param {goog.events.BrowserEvent} e The event.
<a name="line177"></a> * @private
<a name="line178"></a> */
<a name="line179"></a>goog.events.ImeHandler.prototype.handleCompositionStart_ =
<a name="line180"></a>    function(e) {
<a name="line181"></a>  this.handleImeActivate_(e);
<a name="line182"></a>};
<a name="line183"></a>
<a name="line184"></a>
<a name="line185"></a>/**
<a name="line186"></a> * Handles the compositionend event.
<a name="line187"></a> * @param {goog.events.BrowserEvent} e The event.
<a name="line188"></a> * @private
<a name="line189"></a> */
<a name="line190"></a>goog.events.ImeHandler.prototype.handleCompositionEnd_ = function(e) {
<a name="line191"></a>  this.handleImeDeactivate_(e);
<a name="line192"></a>};
<a name="line193"></a>
<a name="line194"></a>
<a name="line195"></a>/**
<a name="line196"></a> * Handles the compositionupdate and text events.
<a name="line197"></a> * @param {goog.events.BrowserEvent} e The event.
<a name="line198"></a> * @private
<a name="line199"></a> */
<a name="line200"></a>goog.events.ImeHandler.prototype.handleTextModifyingInput_ =
<a name="line201"></a>    function(e) {
<a name="line202"></a>  if (this.isImeMode()) {
<a name="line203"></a>    this.processImeComposition_(e);
<a name="line204"></a>  }
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * Handles IME activation.
<a name="line210"></a> * @param {goog.events.BrowserEvent} e The event.
<a name="line211"></a> * @private
<a name="line212"></a> */
<a name="line213"></a>goog.events.ImeHandler.prototype.handleImeActivate_ = function(e) {
<a name="line214"></a>  if (this.imeMode_) {
<a name="line215"></a>    return;
<a name="line216"></a>  }
<a name="line217"></a>
<a name="line218"></a>  // Listens for keyup events to handle unexpected IME keydown events on older
<a name="line219"></a>  // versions of webkit.
<a name="line220"></a>  //
<a name="line221"></a>  // In those versions, we currently use textInput events deactivate IME
<a name="line222"></a>  // (see handleTextInput_() for the reason). However,
<a name="line223"></a>  // Safari fires a keydown event (as a result of pressing keys to commit IME
<a name="line224"></a>  // text) with keyCode == WIN_IME after textInput event. This activates IME
<a name="line225"></a>  // mode again unnecessarily. To prevent this problem, listens keyup events
<a name="line226"></a>  // which can use to determine whether IME text has been committed.
<a name="line227"></a>  if (goog.userAgent.WEBKIT &amp;&amp;
<a name="line228"></a>      !goog.events.ImeHandler.USES_COMPOSITION_EVENTS) {
<a name="line229"></a>    this.keyUpHandler_.listen(this.el_,
<a name="line230"></a>        goog.events.EventType.KEYUP, this.handleKeyUpSafari4_);
<a name="line231"></a>  }
<a name="line232"></a>
<a name="line233"></a>  this.imeMode_ = true;
<a name="line234"></a>  this.dispatchEvent(
<a name="line235"></a>      new goog.events.ImeHandler.Event(
<a name="line236"></a>          goog.events.ImeHandler.EventType.START, e));
<a name="line237"></a>};
<a name="line238"></a>
<a name="line239"></a>
<a name="line240"></a>/**
<a name="line241"></a> * Handles the IME compose changes.
<a name="line242"></a> * @param {goog.events.BrowserEvent} e The event.
<a name="line243"></a> * @private
<a name="line244"></a> */
<a name="line245"></a>goog.events.ImeHandler.prototype.processImeComposition_ = function(e) {
<a name="line246"></a>  this.dispatchEvent(
<a name="line247"></a>      new goog.events.ImeHandler.Event(
<a name="line248"></a>          goog.events.ImeHandler.EventType.UPDATE, e));
<a name="line249"></a>};
<a name="line250"></a>
<a name="line251"></a>
<a name="line252"></a>/**
<a name="line253"></a> * Handles IME deactivation.
<a name="line254"></a> * @param {goog.events.BrowserEvent} e The event.
<a name="line255"></a> * @private
<a name="line256"></a> */
<a name="line257"></a>goog.events.ImeHandler.prototype.handleImeDeactivate_ = function(e) {
<a name="line258"></a>  this.imeMode_ = false;
<a name="line259"></a>  this.keyUpHandler_.removeAll();
<a name="line260"></a>  this.dispatchEvent(
<a name="line261"></a>      new goog.events.ImeHandler.Event(
<a name="line262"></a>          goog.events.ImeHandler.EventType.END, e));
<a name="line263"></a>};
<a name="line264"></a>
<a name="line265"></a>
<a name="line266"></a>/**
<a name="line267"></a> * Handles a key down event.
<a name="line268"></a> * @param {!goog.events.BrowserEvent} e The event.
<a name="line269"></a> * @private
<a name="line270"></a> */
<a name="line271"></a>goog.events.ImeHandler.prototype.handleKeyDown_ = function(e) {
<a name="line272"></a>  // Firefox and Chrome have a separate event for IME composition (&#39;text&#39;
<a name="line273"></a>  // and &#39;compositionupdate&#39;, respectively), other browsers do not.
<a name="line274"></a>  if (!goog.events.ImeHandler.USES_COMPOSITION_EVENTS) {
<a name="line275"></a>    var imeMode = this.isImeMode();
<a name="line276"></a>    // If we&#39;re in IE and we detect an IME input on keyDown then activate
<a name="line277"></a>    // the IME, otherwise if the imeMode was previously active, deactivate.
<a name="line278"></a>    if (!imeMode &amp;&amp; e.keyCode == goog.events.KeyCodes.WIN_IME) {
<a name="line279"></a>      this.handleImeActivate_(e);
<a name="line280"></a>    } else if (imeMode &amp;&amp; e.keyCode != goog.events.KeyCodes.WIN_IME) {
<a name="line281"></a>      if (goog.events.ImeHandler.isImeDeactivateKeyEvent_(e)) {
<a name="line282"></a>        this.handleImeDeactivate_(e);
<a name="line283"></a>      }
<a name="line284"></a>    } else if (imeMode) {
<a name="line285"></a>      this.processImeComposition_(e);
<a name="line286"></a>    }
<a name="line287"></a>  }
<a name="line288"></a>
<a name="line289"></a>  // Safari on Mac doesn&#39;t send IME events in the right order so that we must
<a name="line290"></a>  // ignore some modifier key events to insert IME text correctly.
<a name="line291"></a>  if (goog.events.ImeHandler.isImeDeactivateKeyEvent_(e)) {
<a name="line292"></a>    this.lastKeyCode_ = e.keyCode;
<a name="line293"></a>  }
<a name="line294"></a>};
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * Handles a textInput event.
<a name="line299"></a> * @param {!goog.events.BrowserEvent} e The event.
<a name="line300"></a> * @private
<a name="line301"></a> */
<a name="line302"></a>goog.events.ImeHandler.prototype.handleTextInput_ = function(e) {
<a name="line303"></a>  // Some WebKit-based browsers including Safari 4 don&#39;t send composition
<a name="line304"></a>  // events. So, we turn down IME mode when it&#39;s still there.
<a name="line305"></a>  if (!goog.events.ImeHandler.USES_COMPOSITION_EVENTS &amp;&amp;
<a name="line306"></a>      goog.userAgent.WEBKIT &amp;&amp;
<a name="line307"></a>      this.lastKeyCode_ == goog.events.KeyCodes.WIN_IME &amp;&amp;
<a name="line308"></a>      this.isImeMode()) {
<a name="line309"></a>    this.handleImeDeactivate_(e);
<a name="line310"></a>  }
<a name="line311"></a>};
<a name="line312"></a>
<a name="line313"></a>
<a name="line314"></a>/**
<a name="line315"></a> * Handles the key up event for any IME activity. This handler is just used to
<a name="line316"></a> * prevent activating IME unnecessary in Safari at this time.
<a name="line317"></a> * @param {!goog.events.BrowserEvent} e The event.
<a name="line318"></a> * @private
<a name="line319"></a> */
<a name="line320"></a>goog.events.ImeHandler.prototype.handleKeyUpSafari4_ = function(e) {
<a name="line321"></a>  if (this.isImeMode()) {
<a name="line322"></a>    switch (e.keyCode) {
<a name="line323"></a>      // These keyup events indicates that IME text has been committed or
<a name="line324"></a>      // cancelled. We should turn off IME mode when these keyup events
<a name="line325"></a>      // received.
<a name="line326"></a>      case goog.events.KeyCodes.ENTER:
<a name="line327"></a>      case goog.events.KeyCodes.TAB:
<a name="line328"></a>      case goog.events.KeyCodes.ESC:
<a name="line329"></a>        this.handleImeDeactivate_(e);
<a name="line330"></a>        break;
<a name="line331"></a>    }
<a name="line332"></a>  }
<a name="line333"></a>};
<a name="line334"></a>
<a name="line335"></a>
<a name="line336"></a>/**
<a name="line337"></a> * Returns whether the given event should be treated as an IME
<a name="line338"></a> * deactivation trigger.
<a name="line339"></a> * @param {!goog.events.Event} e The event.
<a name="line340"></a> * @return {boolean} Whether the given event is an IME deactivate trigger.
<a name="line341"></a> * @private
<a name="line342"></a> */
<a name="line343"></a>goog.events.ImeHandler.isImeDeactivateKeyEvent_ = function(e) {
<a name="line344"></a>  // Which key events involve IME deactivation depends on the user&#39;s
<a name="line345"></a>  // environment (i.e. browsers, platforms, and IMEs). Usually Shift key
<a name="line346"></a>  // and Ctrl key does not involve IME deactivation, so we currently assume
<a name="line347"></a>  // that these keys are not IME deactivation trigger.
<a name="line348"></a>  switch (e.keyCode) {
<a name="line349"></a>    case goog.events.KeyCodes.SHIFT:
<a name="line350"></a>    case goog.events.KeyCodes.CTRL:
<a name="line351"></a>      return false;
<a name="line352"></a>    default:
<a name="line353"></a>      return true;
<a name="line354"></a>  }
<a name="line355"></a>};
<a name="line356"></a>
<a name="line357"></a>
<a name="line358"></a>/** @override */
<a name="line359"></a>goog.events.ImeHandler.prototype.disposeInternal = function() {
<a name="line360"></a>  this.handler_.dispose();
<a name="line361"></a>  this.keyUpHandler_.dispose();
<a name="line362"></a>  this.el_ = null;
<a name="line363"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line364"></a>};
</pre>


</body>
</html>
